import { message } from 'antd';
import {
    candiDelete,
    addCandidate,
    addFilter,
    changeCandiState,
    getCandidateDetail
} from '@/services/myRecruit/candidate';
import { getTypeList } from '@/services/components/userform';
import { featchApply } from '@/services/myRecruit/interview';

export default {
    namespace: 'common',
    state: {
        stageList: [], // 招聘阶段
        scheduleList: [], // 招聘操作状态字段
        candiDetail: {}, // 候选人详情信息
        asianList: [] // 籍贯下拉数据获取
    },
    effects: {
        // 招聘阶段
        *fetchStageList({ payload }, { call, put }) {
            const result = yield call(getTypeList, payload);
            const { data } = result;
            if (result && result.code === 10000) {
                const newData = [];
                data.forEach(item => {
                    newData.push({
                        value: item.dictValue,
                        text: item.dictName
                    });
                });
                yield put({
                    type: 'save',
                    payload: {
                        stageList: newData
                    }
                });
            }
        },
        // 招聘状态
        *fetchscheduleList({ payload }, { call, put }) {
            const result = yield call(getTypeList, payload);
            const { data } = result;
            if (result && result.code === 10000) {
                const newData = [];
                data.forEach(item => {
                    newData.push({
                        value: item.dictValue,
                        text: item.dictName
                    });
                });
                yield put({
                    type: 'save',
                    payload: {
                        scheduleList: newData
                    }
                });
            }
        },
        // 添加候选人
        *addCandidate({ payload, success }, { call }) {
            const result = yield call(addCandidate, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success();
            } else {
                message.error(result.msg);
            }
        },
        // 候选人删除
        *candidateDelete({ payload, success }, { call }) {
            // alert(22);
            const result = yield call(candiDelete, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success();
            }
        },
        // 发起用人部门筛选
        *addFilter({ payload, success }, { call }) {
            // const { companyId } = payload;
            const result = yield call(addFilter, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success();
            } else {
                message.error('推荐筛选官失败');
            }
        },
        // 设置筛选结果--成功后刷新列表
        *setResult({ payload, success }, { call }) {
            // const { companyId } = payload;
            const result = yield call(changeCandiState, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // eslint-disable-next-line no-unused-expressions
                success && success();
            } else {
                message.error('设置筛选结果失败');
            }
        },
        // 请求候选人详情信息
        *candidateDetail({ payload }, { call, put }) {
            const result = yield call(getCandidateDetail, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        candiDetail: result.data
                    }
                });
            }
        },
        // 发起offer申请
        *featchApply({ payload, callback }, { call }) {
            const result = yield call(featchApply, payload);
            if (result.code === 10000) {
                if (callback) callback(result.data);
            }
        },
        // 籍贯下拉
        *fetchAsisn({ payload }, { call, put }) {
            const result = yield call(getTypeList, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        asianList: result.data
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
